package dwl.graduation.design.service.impl;

import dwl.graduation.design.domain.*;
import dwl.graduation.design.mapper.UserInfoMapper;
import dwl.graduation.design.mapper.UserRoleMapper;
import dwl.graduation.design.service.UserService;
import dwl.graduation.design.utils.PageResult;
import dwl.graduation.design.utils.QueryPageBean;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    @PreAuthorize("hasRole('ADMIN')")
    public PageResult findUserList(QueryPageBean queryPageBean) {
        if (!Objects.isNull(queryPageBean.getCurrentPage()) && !Objects.isNull(queryPageBean.getPageSize())) {
            queryPageBean.setCurrentPage((queryPageBean.getCurrentPage() - 1) * queryPageBean.getPageSize());
        }
        List<UserInfo> userInfoList = userInfoMapper.getUserInfoList(queryPageBean);
        Long count = userInfoMapper.getListCount(queryPageBean);
        List<UserResponse> userResponseList = new ArrayList<>();
        userInfoList.parallelStream().forEach(item->{
            UserResponse userResponse = new UserResponse();
            BeanUtils.copyProperties(item, userResponse);
            userResponse.setRoleId(userRoleMapper.selectRoleIdByUserId(item.getId()));
            userResponseList.add(userResponse);
        });
        return PageResult.builder().rows(userResponseList).total(count).build();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @PreAuthorize("hasRole('ADMIN')")
    public boolean insertUserInfo(UserInfoInsertRequest request) {
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(request, userInfo);
        int flag = userInfoMapper.insertSelective(userInfo);
        UserRoleKey userRoleKey = new UserRoleKey();
        userRoleKey.setRoleId(Integer.parseInt(request.getRoleId()));
        userRoleKey.setUserId(userInfo.getId());
        userRoleMapper.insertSelective(userRoleKey);
        return flag > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @PreAuthorize("hasRole('ADMIN')")
    public void deleteUserInfo(Integer id) {
        UserRoleExample userRoleExample = new UserRoleExample();
        userRoleExample.createCriteria().andUserIdEqualTo(id);
        List<UserRoleKey> userRoleKeys = userRoleMapper.selectByExample(userRoleExample);
        if (userRoleKeys.size()>0){
           userRoleMapper.deleteByExample(userRoleExample);
        }
        userInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @PreAuthorize("hasRole('ADMIN')")
    public boolean updateUserInfo(UserInfoUpdateRequest userInfoUpdateRequest) {
        UserInfoExample userInfoExample = new UserInfoExample();
        userInfoExample.createCriteria().andIdEqualTo(userInfoUpdateRequest.getId());
        List<UserInfo> userInfoList = userInfoMapper.selectByExample(userInfoExample);
        if (userInfoList.size() == 0) {
            return false;
        }
        if (!Objects.isNull(userInfoUpdateRequest.getRoleId())){
            UserRoleExample userRoleExample = new UserRoleExample();
            userRoleExample.createCriteria().andUserIdEqualTo(userInfoUpdateRequest.getId());
            UserRoleKey userRoleKey = new UserRoleKey();
            userRoleKey.setUserId(userInfoUpdateRequest.getId());
            userRoleKey.setRoleId(userInfoUpdateRequest.getRoleId());
            userRoleMapper.updateByExampleSelective(userRoleKey,userRoleExample);
        }
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(userInfoUpdateRequest, userInfo);
        UserInfoExample userInfoUpdateExample = new UserInfoExample();
        userInfoUpdateExample.createCriteria().andIdEqualTo(userInfo.getId());
        int flag = userInfoMapper.updateByExampleSelective(userInfo, userInfoUpdateExample);
        return flag > 0;
    }

    @Override
    public UserInfo findUserByUserName(String userName) {
        return userInfoMapper.findUserByUserName(userName);
    }

    @Override
    public UserResponse findUserById(Integer id) {
        UserResponse userResponse = new UserResponse();
        BeanUtils.copyProperties(userInfoMapper.findUserById(id), userResponse);
        userResponse.setRoleId(userRoleMapper.selectRoleIdByUserId(id));
        return userResponse;
    }

    @Override
    public boolean insertRoleForUser(InsertRoleForUserRequest insertRoleForUserRequest) {
        UserRoleKey userRoleKey = new UserRoleKey();
        BeanUtils.copyProperties(insertRoleForUserRequest, userRoleKey);
        int flag = userRoleMapper.insertSelective(userRoleKey);
        return flag > 0;
    }
}


